<?php
if(!defined('BASEPATH'))
	exit('No direct script access allowed');

class Login extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index() {
		$this -> load -> view('login_view');
	}

	function logout() {
		$this -> session -> sess_destroy();
		redirect('welcome');
	}

	function validate_credentials() {

		$query = $this -> _validate();

		if($query) // if the user's credentials validated...
		{
			$data = array('username' => $this -> input -> post('username'), 'is_logged_in' => true);
			$this -> session -> set_userdata($data);
			echo "tomalabarbona!";
			redirect('admin');

		} else // incorrect username or password
		{
			redirect('welcome');
		}
	}

	function _validate() {

		$this -> db -> select('username, full_name');
		$this -> db -> from('users');
		$this -> db -> where('username', $this -> input -> post('username'));
		$this -> db -> where('password', $this -> input -> post('password'));

		$query = $this -> db -> get();
		echo $this -> db -> last_query();

		if($query -> num_rows == 1) {
			echo "valid username and pwd";
			return true;
		}

	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
